package com.chutoutian.controller;

import com.chutoutian.common.Code;
import com.chutoutian.po.User;
import com.chutoutian.service.UserService;
import com.chutoutian.vo.Cart;
import com.chutoutian.vo.ResponseMessage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping(value = "/v1")
public class LoginController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Object login(HttpSession session) {
        User user = userService.login("AB86");
        session.setAttribute("user", user);
        Cart cart = new Cart();
        cart.setUserId(user.getId());
        cart.setCartItemMap(new HashMap<>());
        session.setAttribute("cart", cart);
        return new ResponseMessage.Builder().code(Code.CODE_200).data(user).bulid();
    }

}
